﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace TeamBuy.AccessService
{
    public class TeamBuyDealService : Common.ITeamBuyDeal
    {
        public DateTime SetDealFinishedDate(int dealID,int currentStatus, DateTime dateFinished)
        {
            if (currentStatus == 2 || currentStatus == 3)
            {
                return new DateTime();
            }
            else
            {
                int updatedDateFinished;
                updatedDateFinished = TeamBuy.AccessService.AccessHelper.ExecuteCommand("update S_Deal set DateFinished='" + dateFinished + "' where DealID=" + dealID);
                if (updatedDateFinished == 1)
                {
                    return dateFinished;
                }
                else
                {
                    return new DateTime();
                }
            }
        }

        public int SetDealCurrentStatus(int dealID, int oldStatus, int currentStatus)
        {
            if (oldStatus == currentStatus)
            {
                return oldStatus;
            }
            else
            {
                int updateStatus;
                updateStatus = TeamBuy.AccessService.AccessHelper.ExecuteCommand("update S_Deal set DealStatus=" + currentStatus + " where DealID=" + dealID);
                if (updateStatus == 1)
                {
                    return currentStatus;
                }
                else
                {
                    return -1;
                }
            }
        }


        public bool SetDealSuccessedDate(int dealID, DateTime dateSuccessed)
        {
            int updateSuccessed;
            updateSuccessed = TeamBuy.AccessService.AccessHelper.ExecuteCommand("update S_Deal set DateSuccessed='" + dateSuccessed + "' where DealID=" + dealID);
            if (updateSuccessed == 1)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        public System.Data.DataTable GetDealByID(int dealID)
        {
            DataTable dt = TeamBuy.AccessService.AccessHelper.GetDataSet("select * from S_Deal where DealID=" + dealID);
            return dt;
        }


        public int PayDeal(int dealID, int quantity)
        {
            int updateSuccessed;
            updateSuccessed = TeamBuy.AccessService.AccessHelper.ExecuteCommand("update S_Deal set PayNumber=PayNumber+" + quantity + " where DealID=" + dealID);
            if (updateSuccessed == 1)
            {
                return 1;
            }
            else
            {
                return 0;
            }
        }


        public int GetLeftDealNumber(int dealID)
        {
            return TeamBuy.AccessService.AccessHelper.GetScalar("select DealNumber-PayNumber from S_Deal where DealID=" + dealID);
        }
    }
}
